package com.yanyeori.nacosb.controller;

import com.yanyeori.framework.core.model.WebResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 测试接口
 * @date 20/8/07 10:29
 */
@RestController
@RequestMapping("/testController")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    private final String http = "http://";
    private final String nacosAServiceName = "nacos-a";

    @PostMapping("/rest")
    public WebResponse rest() {
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("param", "nacos-b请求nacos-a的参数");
        WebResponse response = restTemplate.postForObject(http + nacosAServiceName + "/testController/get", params, WebResponse.class);
        return response;
    }

    @PostMapping("/get")
    public WebResponse get(@RequestParam("param") String param) {
        return WebResponse.ok().data("b收到请求：" + param);
    }
}
